<?php
$maxwight = get_shiping_max_weight();
$shipingprice = get_shiping_price();
$priceforunit = get_shiping_price_for_unit();

if ($product) {
    //echo var_dump($product);
    ?>
    <h1><span></span><?= $product->name ?></h1>

    <ul class="breadcrumb">
        <li><a href="<?= base_url() ?>"><?= lang('label_home') ?></a></li>
        <li><a href="<?= base_url("products/category/$product->mcat_id") ?>"><?php
                $maintitle = unserialize($product->maintitle);
                echo (isset($maintitle[LANG])) ? $maintitle[LANG] : ((LANG == 'ar') ? $maintitle['en'] : $maintitle['ar']);
                ?></a></li>
        <li><a href="<?= base_url("products/category/$product->subcat_id") ?>"><?php
                $subtitle = unserialize($product->subtitle);
                echo (isset($subtitle[LANG])) ? $subtitle[LANG] : ((LANG == 'ar') ? $subtitle['en'] : $subtitle['ar']);
                ?></a></li>
        <li><?= $product->name ?></li>
    </ul>


    <div class="page">
        <link rel="stylesheet" href="<?php echo str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']) ?>/<?php echo APPPATH ?>views/<?php echo $this->themes->Cms_themes() ?>/player/video-js.css">
        <script src="<?php echo str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']) ?>/<?php echo APPPATH ?>views/<?php echo $this->themes->Cms_themes() ?>/player/video.js"></script>

        <div class="product_item">
            <div class="product_item_info">
                <h1><?= $product->name ?></h1>
                <h2><?= lang('label_by') ?> : <a href="<?= base_url("store/index/$product->store_id") ?>"><?= $product->value ?></a></h2>

                <?php
                $size = unserialize($product->size);
                if (count($size) > 1) {
                    ?>
                    <div class="product_item_color2">
                        <ul>
                            <li style="vertical-align:top;"><?= lang('label_product_size') ?> :</li>
                            <?php foreach ($size as $k => $v) { ?>
                                <li><samp><?= strtoupper($v) ?></samp></li>
                            <?php } ?>
                        </ul>
                    </div>
                <?php } ?>
                <?php
                $color = unserialize($product->colors);
                if (count($color) > 1) {
                    ?>
                    <div class="product_item_color">
                        <ul>
                            <li style="width:auto; vertical-align:top;"><?= lang('label_product_color') ?> :</li>
                            <?php foreach ($color as $k => $v) { ?>
                                <li class="<?= $v ?>"></li>
                            <?php } ?>
                        </ul>
                    </div>
                <?php } ?>
                <div class="product_item_color2">
                    <ul>
                        <li><?= lang('label_menu_link_trade') ?> : </li>
                        <li><img src="<?= base_url(TRADE_PATH . $product->tradeimage) ?>" alt=""></li>
                    </ul>
                </div>
                <div class="product_item_color">
                    <?php if ($product->discount_precentage > 0) { ?>
                        <span><?= lang('label_product_old_price') ?> : </span>
                        <strong><strike><?= $product->price ?></strike> <?= lang('label_rial') ?></strong>
                    <?php } else { ?>
                        <span><?= lang('label_product_price') ?> : </span>
                        <strong><?= $product->price ?> <?= lang('label_rial') ?></strong>            
                    <?php } ?>
                </div>
                <div class="product_item_color2">
                    <span><?= lang('label_prod_condition') ?> : </span>
                    <strong><?= lang('label_condition_' . strtolower($product->condition)) ?></strong>
                </div>
                <div class="product_item_color" style="margin-bottom:0;">
                    <span><?= lang('label_product_quentity') ?> : </span>
                    <strong><?= $product->quantity ?></strong>
                </div>


            </div>

            <div class="product_item_img">
                <?php
                $images = unserialize($product->images);
                $nodef = TRUE;
                ?>

                <div class="itemimg">
                    <div><img src="<?php
                        if (count($images) > 0) {
                            foreach ($images as $k => $v) {
                                if ($v[1] == 'yes') {
                                    $nodef = false;
                                    $defimage = $v[0];
                                    echo $image = base_url(PRODUCT_PATH . $v[0]);
                                }
                            } if ($nodef) {
                                echo $image = base_url(PRODUCT_PATH . $images[0][0]);
                                 $defimage = $images[0][0];
                            }
                        } else {
                            echo $image = str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']) . '/' . APPPATH . 'views/' . $this->themes->Cms_themes() . '/images/noimage_' . LANG . '.gif';
                        }
                        ?>" alt="" id="proddef"></div>
                              <?php if ($product->discount_precentage > 0) { ?>
                        <span class="stitch"><b> &nbsp;%</b><br /><?= $product->discount_precentage ?></span>
                    <?php } ?>
                </div>

                <ul class="allitemimg">
                    <?php foreach ($images as $k => $v) { ?>
                        <li><img src="<?= base_url(PRODUCT_PATH . $v[0]) ?>" alt="" class="prodthumbnail" /></li>
                    <?php } ?>
                </ul>
            </div>

        </div>

        <div class="product_item2">
            <div class="addcart">
                <h2><?= ($product->discount_precentage > 0) ? $product->discount_price : $product->price; ?> <span><?= lang('label_rial') ?></span></h2>

                <div class="addcartsubmit submit">
                    <?php if ($product->owner == $this->user_id) { ?>
                        <input name="submit" value="<?= lang('label_edit_product') ?>" type="button" onclick="location.href = '<?= base_url("products/edit/$product->id") ?>'"  />
                    <?php } else { ?>
                        <span></span>
                        <?php if (check_product_incart($product->id)) { ?>
                            <input name="submit" value="<?= lang('label_remove_cart') ?>" type="button" onclick="location.href = '<?= base_url("cart/remove/$product->id") ?>'"  />
                        <?php } else { ?>
                            <input name="submit" value="<?= lang('label_add_cart') ?>" type="button" onclick="location.href = '<?= base_url("cart/add/$product->id") ?>'"  />
                        <?php } ?>
                    <?php } ?>
                </div>
            </div>

            <div class="addwish">
                <?= lang('label_share') ?> : 
                <a class="ic_tw" href="javascript:void(0)" onclick="
                                    window.open(
                                            'https://twitter.com/intent/tweet?original_referer=' + encodeURIComponent('<?= base_url() ?>') + '&text=' + encodeURIComponent('<?= $product->descrebtion ?>') + '&tw_p=tweetbutton&url=' + encodeURIComponent(location.href),
                                            'facebook-share-dialog',
                                            'width=626,height=436');
                                    return false;">&nbsp;</a> 
                <a class="ic_fb" href="javascript:void(0)" onclick="
                                    window.open(
            'http://www.facebook.com/sharer/sharer.php?s=100&p[url]='+location.href+'&p[images][0]=<?= $image ?>&p[title]=<?= $product->name ?>&p[summary]=<?= $product->descrebtion ?>',
            //'https://www.facebook.com/sharer/sharer.php?u=' ,
                                            'facebook-share-dialog',
                                            'width=626,height=436');
                                    return false;">&nbsp;</a>
                   <?php if ($listid = get_in_wlist($product->id)) { ?>
                    <a class="atwlist" href="<?= base_url('wishlist/remove/' . $listid) ?>"><?= lang('label_remove_fromwlist') ?></a>
                <?php } else { ?>
                    <a class="atwlist" href="<?= base_url('wishlist/add/' . $product->id) ?>"><?= lang('label_add_towlist') ?></a>
                <?php } ?>
            </div>
        </div>


        <div id="item_description">
            <div class="item_tap">
                <ul>
                    <?php if ($product->additional != 0) { ?>
                        <?php
                        $catadditional = get_cat_additional($product->subcat_id);
                        $productadditional = get_product_additional($product->id);
                        if ($catadditional && $productadditional) {
                            ?>
                            <li><a href="#details"><?= lang('label_details') ?></a></li>
                            <?php
                        }
                    }
                    ?>
                    <?php if ($product->descrebtion) { ?>
                        <li><a href="#description"><?= lang('label_product_desc') ?></a></li>
                    <?php } ?>
                    <?php if ($product->vedio) { ?>
                        <li><a href="#video"><?= lang('label_product_vedio') ?></a></li>
                    <?php } ?>
                    <li><a href="#shipreturns"><?= lang('label_shiping_returns') ?></a></li>

                </ul>
            </div>

            <?php if ($product->descrebtion) { ?>
                <div id="description"><?= $product->descrebtion ?></div>
            <?php } ?>

            <?php
            if ($product->vedio) {
                $url = $product->vedio;
                if (preg_match('%(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $result)) {
                    $video_id = $result[1];
                }
                ?>

                <div id="video">
                    <?php if (isset($video_id)) {
                        ?>
                        <iframe id="player" type="text/html" width="640" height="390"
                                src="http://www.youtube.com/embed/<?= $video_id ?>?enablejsapi=1&origin=<?= $product->vedio ?>"
                                frameborder="0"></iframe>
                            <?php } ?>
                </div>
            <?php } ?>

            <div id="shipreturns">
                <table id="tablestyle2">
                    <thead>
                    <th colspan="2"><?= lang('label_prod_shiping_for') ?></th>
                    </thead>

                    <tbody>
                        <tr>
                            <td><?= lang('label_prod_shiping_support') ?> </td>
                            <td>
                                <?php
                                if ($product->shiping_weghite <= $maxwight) {
                                    echo (double) $shipingprice;
                                } else {
                                    $weghit = (double) $maxwight - 10;
                                    echo $shipingprice + $priceforunit * $weghit;
                                }
                                ?><?= lang('label_rial') ?>
                            </td>
                        </tr>
                        <tr>
                            <td><?= lang('label_prod_shiping_unsupport') ?></td>
                            <td>
                                <?= $product->shiping_unsuport ?> <?= lang('label_rial') ?>
                            </td>
                        </tr>
                    </tbody>
                </table>

                <br />

                <?php if ($product->returns_policy == 'YES') { ?>
                    <table id="tablestyle2">
                        <thead>
                        <th colspan="2"><?= lang('label_returns_policy') ?> (<span style="font-weight:normal;"><?= lang('label_prod_returns_yes') ?></span>)</th>
                        </thead>
                        <tbody>
                            <tr>
                                <td><?= lang('label_returns_days') ?></td>
                                <td><?= $product->returns_days . ' ' . lang('label_day') ?></td>
                            </tr>
                            <?php if ($product->returns_note) { ?>
                                <tr>
                                    <td><?= lang('label_note') ?></td>
                                    <td><?= $product->returns_note ?></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                <?php } ?>
                <!--
                                < ?php } else { ?>
                    <span style="font-weight: bold">< ?= lang('label_prod_returns_no') ?> </span>
                
                -->



            </div>

            <?php if ($product->additional != 0) { ?>
                <?php if ($catadditional && $productadditional) { ?>
                    <div id="details">
                        <table id="tablestyle2">
                            <tbody>
                                <?php foreach ($catadditional as $catadd) { ?>
                                    <?php foreach ($productadditional as $prductadd) { ?>
                                        <?php if ($prductadd->code == $catadd->code && $prductadd->value != '') { ?>
                                            <tr>
                                                <td><?php
                                                    $title = unserialize($catadd->title);
                                                    $title = (isset($title[LANG])) ? $title[LANG] : ((LANG == 'ar') ? $title['en'] : $title['ar']);
                                                    echo $title;
                                                    ?></td>
                                                <td><?php
                                                    if (in_array($catadd->type, array('text', 'textarea')))
                                                        echo $prductadd->value;
                                                    elseif ($catadd->type == 'list') {
                                                        $values = unserialize($catadd->data);
                                                        $values = (isset($values[LANG])) ? $values[LANG] : ((LANG == 'ar') ? $values['en'] : $values['ar']);
                                                        $values = explode('|', $values);
                                                        if ($values) {
                                                            echo $values[$prductadd->value];
                                                        }
                                                    } elseif ($catadd->type == 'checkbox') {
                                                        $values = unserialize($catadd->data);
                                                        $values = (isset($values[LANG])) ? $values[LANG] : ((LANG == 'ar') ? $values['en'] : $values['ar']);
                                                        $values = explode('|', $values);
                                                        if ($values) {
                                                            $productvalues = unserialize($prductadd->value);
                                                            if (count($productvalues) > 0) {
                                                                echo '<ul>';
                                                                foreach ($productvalues as $pr => $vl) {
                                                                    echo '<li> - ' . $values[$vl] . '</li>';
                                                                }
                                                                echo '</ul>';
                                                            }
                                                        }
                                                    }
                                                    ?></td>
                                            </tr>
                                        <?php } ?>
                                    <?php } ?>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                    <?php
                }
            }
            ?>

        </div>
    </div>
    <meta property="og:title" content="<?= $product->name ?>" />
    <meta property="og:description" content="<?= $product->descrebtion ?>" />
    <meta property="og:image" content="<?= $image ?>" />
    <script type="text/javascript">
                            $(document).ready(function() {
                                $('.prodthumbnail').click(function() {
                                    $('#proddef').attr('src', $(this).attr('src'))
                                })
                            })
    </script>
    <?php
} else {
    redirect();
}